package com.gxuwz.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class ServerSocketChannelDemo {
    public static void main(String[] args) throws IOException, InterruptedException {
        int port=8888;

        ByteBuffer buffer=ByteBuffer.wrap("hello at".getBytes());

        ServerSocketChannel ssc=ServerSocketChannel.open();
        ssc.socket().bind(new InetSocketAddress(port));
        //非阻塞模式
        ssc.configureBlocking(false);


        while (true){
            //接受与此通道的套接字建立的连接
            SocketChannel sc= ssc.accept();
            if (sc==null){
                System.out.println("wait connect");
                System.out.println("null");
                Thread.sleep(1000);
            }
            else {
                System.out.println("connect"+sc.socket().getRemoteSocketAddress());
                buffer.rewind();//指针0
                //
                sc.write(buffer);
                sc.close();
            }

        }
    }
}
